package com.sdhs.web.controller.workbench;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.sdhs.common.annotation.Log;
import com.sdhs.common.core.controller.BaseController;
import com.sdhs.common.core.domain.AjaxResult;
import com.sdhs.common.core.page.TableDataInfo;
import com.sdhs.common.enums.BusinessType;
import com.sdhs.common.enums.MessageInfoDictEnum;
import com.sdhs.common.utils.poi.ExcelUtil;
import com.sdhs.system.domain.MessageInfoDO;
import com.sdhs.system.domain.dto.MessageInfoDTO;
import com.sdhs.system.domain.vo.MessageInfoVO;
import com.sdhs.system.service.IMessageInfoService;

/**
 * 消息Controller
 *
 * @author mz
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/message/info")
public class MessageInfoController extends BaseController {
    @Autowired
    private IMessageInfoService messageInfoService;

    /**
     * 查询消息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MessageInfoDTO messageInfoDTO, String beginDate, String endDate) {
        startPage();
        messageInfoDTO.getParams().put("beginDate", beginDate);
        messageInfoDTO.getParams().put("endDate", endDate);
        List<MessageInfoVO> list = messageInfoService.selectMessageInfoList(messageInfoDTO);
        return getDataTable(list);
    }


    /**
     * 查询消息列表-不分页
     */
    @GetMapping("/onlyList")
    public AjaxResult onlyList(MessageInfoDTO messageInfoDTO, String beginDate, String endDate) {
        messageInfoDTO.getParams().put("beginDate", beginDate);
        messageInfoDTO.getParams().put("endDate", endDate);
        List<MessageInfoVO> list = messageInfoService.selectMessageInfoList(messageInfoDTO);
        return success(list);
    }

    /**
     * 导出消息列表
     */
    @PreAuthorize("@ss.hasPermi('message:info:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageInfoDTO messageInfoDTO) {
        List<MessageInfoVO> list = messageInfoService.selectMessageInfoList(messageInfoDTO);
        ExcelUtil<MessageInfoVO> util = new ExcelUtil<MessageInfoVO>(MessageInfoVO.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,@RequestParam(value = "type", required = false) String type) {
        if("safety".equals(type)){
            return success(messageInfoService.selectMessageInfoByNoticeId(id));
        } else {
            return success(messageInfoService.selectMessageInfoById(id)) ;
        }
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('message:info:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessageInfoDO messageInfo) {
        return toAjax(messageInfoService.insertMessageInfo(messageInfo));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('message:info:edit')")
    @Log(title = "修改消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MessageInfoDO messageInfo) {
        return toAjax(messageInfoService.updateMessageInfo(messageInfo));
    }


    /**
     * 修改当前用户的所有消息状态为已读
     */
    @PreAuthorize("@ss.hasPermi('message:info:updateAllStatus')")
    @Log(title = "修改当前用户的所有消息状态为已读", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAllStatus")
    public AjaxResult updateAllStatus() {
        return toAjax(messageInfoService.updateAllMsg(MessageInfoDictEnum.READ_FLAG__READ.getCode()));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('message:info:remove')")
    @Log(title = "删除消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(messageInfoService.deleteMessageInfoByIds(ids));
    }


    /**
     * 未读消息数量
     */
//    @PreAuthorize("@ss.hasPermi('message:info:getUnreadCount')")
//    @GetMapping(value = "/getUnreadCount")
//    public AjaxResult getUnreadCount() {
//        return success(messageInfoService.count(new LambdaQueryWrapper<MessageInfoDO>()
//                .eq(MessageInfoDO::getReceiverUserName, SecurityUtils.getUsername())
//                .eq(MessageInfoDO::getStatus, MessageInfoDictEnum.READ_FLAG__UN_READ.getCode())
//        ));
//    }
}
